package com.easyidea.its.domain.repository.department.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;


import com.easyidea.its.domain.model.system.Department;
import com.easyidea.its.domain.model.system.PassPort;
import com.easyidea.its.domain.repository.base.impl.BaseRepositoryImpl;
import com.easyidea.its.domain.repository.department.PassPortRepository;

public class PassPortRepositoryImpl extends BaseRepositoryImpl<PassPort> implements PassPortRepository {

	public List<PassPort> getPassPorts(PassPort passport, int startAtIndex, int pageSize) {
		Criteria criteria =  getPassPortsCriteria(passport);
		if(startAtIndex > 0) {
			criteria.setFirstResult(startAtIndex);
		}
		if (pageSize > 0) {
			criteria.setMaxResults(pageSize);
		}
		return criteria.list();
	}

	public int getPassPortsCount(PassPort passport) {
		Criteria criteria =  getPassPortsCriteria(passport);
		criteria.setProjection(Projections.count("id"));
		return (Integer)criteria.uniqueResult();
	}

	public boolean hasSamePassPortName(String passPortName, Integer id) {
		Criteria criteria = createUndeleteCriteria();
		criteria.add(Restrictions.eq("passPortName", passPortName));
		criteria.add(Restrictions.ne("id", id));
		List<PassPort> ds = criteria.list();
		if (ds != null && ds.size() > 0) {
			return true;
		}
		return false;
	}

	 private Criteria getPassPortsCriteria(PassPort passport) {
	    	Criteria criteria = createUndeleteCriteria();
	    	if(passport.getPassPortName() != null && !passport.getPassPortName().equals("")) {
	    		criteria.add(Restrictions.ilike("passPortName", passport.getPassPortName(), MatchMode.START));	
	    	}	    	
	    	return criteria;
	 }
}
